//
//  DECXUploadPictureViewController.m
//  DECXInStore
//
//  Created by 程强 on 15/12/9.
//  Copyright © 2015年 程强. All rights reserved.
//

#import "DECXUploadPictureViewController.h"
#import "DECXUploadIPictureItem.h"
#import "MKUIImagePickerOperation.h"
#import "AFNetworking.h"
#import "KKFileManager.h"

#define PictureCount 1
#define Frame1 CGRectMake(ScreenWidth/2.0-(ScreenWidth-40)/2.0, 20, (ScreenWidth-40), (ScreenWidth-40))
#define Frame2 CGRectMake(40+(ScreenWidth-20*3)/2.0, 20, (ScreenWidth-20*3)/2.0, (ScreenWidth-20*3)/2.0)
#define Frame3 CGRectMake(20, 40+(ScreenWidth-20*3)/2.0, (ScreenWidth-20*3)/2.0, (ScreenWidth-20*3)/2.0)
#define Frame4 CGRectMake(40+(ScreenWidth-20*3)/2.0, 40+(ScreenWidth-20*3)/2.0, (ScreenWidth-20*3)/2.0, (ScreenWidth-20*3)/2.0)
#define Frame5 CGRectMake(20, 60+(ScreenWidth-20*3), (ScreenWidth-20*3)/2.0, (ScreenWidth-20*3)/2.0)

@interface DECXUploadPictureViewController ()
@property (strong, nonatomic) UIScrollView *backScrollView;
@property (strong, nonatomic) DECXPictureFile *currentFile;
@property (strong, nonatomic) DECXUploadIPictureItem *item;
@end

@implementation DECXUploadPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.uploadAgainBtn.hidden = YES;
    self.uploadSuccessBtn.hidden = YES;
    
    self.linkBtnTop.constant = ScreenWidth-10;
    
    
    self.backScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
//    [self.view addSubview:_backScrollView];
    
    NSInteger itemCount = 0;
    if (self.uploadType == UploadPictureControllerTypeProductLogo) {
        itemCount = PictureCount;
        self.linkBtn.hidden = NO;
    }else{
        itemCount = 1;
        self.linkBtn.hidden = YES;
    }
    
    NSArray *frameAry = @[[NSValue valueWithCGRect:Frame1],[NSValue valueWithCGRect:Frame2],[NSValue valueWithCGRect:Frame3],[NSValue valueWithCGRect:Frame4],[NSValue valueWithCGRect:Frame5]];
    
    for (int i = 0; i<itemCount; i++) {
        
        weak_Self(self);
        self.item = [[DECXUploadIPictureItem alloc]initWithFrame:[frameAry[i] CGRectValue]];
        _item.pictureType = self.uploadType;
        __weak typeof(DECXUploadIPictureItem) *weakItem = _item;
        [self.view addSubview:_item];
        BOOL edit = NO;
        if (self.uploadType == UploadPictureControllerTypeProductLogo) {
            edit = YES;

            if ([DECXGlobal shareInstance].productLogo) {
                _item.picture.image =[DECXGlobal shareInstance].productLogo;
                self.uploadAgainBtn.hidden = NO;
            }
        }else{
            edit = NO;

            if ([DECXGlobal shareInstance].productProve) {
                _item.picture.image =[DECXGlobal shareInstance].productProve;
                self.uploadAgainBtn.hidden = NO;
            }
        }
        _item.selectMediaBlock = ^(MediaType type){
            [weakSelf showCamera:type edit:edit success:^(UIImage *originalImage, UIImage *compressionImage) {
                weakItem.picture.image = originalImage;
                
                [weakSelf showHUD];
                [weakItem startUpload:originalImage thumImage:compressionImage success:^(DECXPictureFile *file) {
                    weakSelf.currentFile = file;
                    ToastShowInWindow(@"上传成功");
                    weakSelf.uploadAgainBtn.hidden = NO;
                    weakSelf.uploadSuccessBtn.hidden = NO;
                } failure:^(NSString *msg) {
                    weakSelf.uploadAgainBtn.hidden = NO;
                    ToastShowInWindow(msg);
                } complete:^{
                    [weakSelf hideHUD];
                }];
            }];
        };

    }
    
    [self.backScrollView setContentSize:CGSizeMake(ScreenWidth, ScreenHeight)];
}
- (void)addLogo:(UIImage *)image
{
    UIView *bView = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth, 0, image.size.width, image.size.height)];
    [self.view addSubview:bView];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    imageV.image = image;
    [bView addSubview:imageV];
    
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(imageV.frame.size.width-70, 0, 70, 70)];
    [bView addSubview:logo];
    logo.image = LoadImage(@"wuLit.png");
    
    UIImage *finishImage = [QFMultimedia captureScreen:bView];
    
    NSData *data = UIImagePNGRepresentation(finishImage);
    KKFileManager *manager = [KKFileManager shareInstance];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"testimage"];
    NSLog(@"path == %@",path);
    [manager writeFilePath:path data:data];

    
}
- (void)showCamera:(MediaType)type edit:(BOOL)edit success:(void(^)(UIImage *originalImage, UIImage *compressionImage))block
{
    weak_Self(self);
    MKUIImagePickerOperation *operation = [MKUIImagePickerOperation shareInstance];
    [operation showImagePickerControllerWithType:type edit:edit success:^(UIImagePickerController *imagePickerController) {
        [weakSelf presentViewController:imagePickerController animated:YES completion:nil];
    }];
    operation.finishBlock = ^(UIImage *originalImage, UIImage *compressionImage){
        block(originalImage,compressionImage);
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    };
}
- (IBAction)uploadAgainClick:(id)sender
{
    [_item click:nil];
}
- (IBAction)uploadSuccessClick:(id)sender
{
    [self gotoBack];
}
- (void)gotoBack
{
    if (self.uploadBlock) {
        self.uploadBlock(self.currentFile);
    }
    [super gotoBack];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
